unit fMeshBuild;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  Vcl.Imaging.Jpeg,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,

  GLS.VectorTypes,
  GLS.Scene,
  GLS.Objects,
  GLS.Coordinates,
  GLS.SceneViewer,
  GLS.VectorGeometry,

  GLS.BaseClasses,
  GLS.Material,
  GLS.FileOBJ,
  GLS.HUDObjects,

  uMesh;

type
  TfMain = class(TForm)
    Scene: TGLScene;
    Viewer: TGLSceneViewer;
    Camera: TGLCamera;
    Target: TGLDummyCube;
    MatLib: TGLMaterialLibrary;
    GLSLogo: TGLHUDSprite;
    procedure FormCreate(Sender: TObject);
    procedure ViewerMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ViewerMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure FormDestroy(Sender: TObject);
    procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
      WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
    procedure FormResize(Sender: TObject);
  private

  public
    Map: TMap;
    mx, my: Integer;
  end;

var
  fMain: TfMain;

//-------------------------------------------
implementation
//-------------------------------------------

{$R *.dfm}

procedure TfMain.FormCreate(Sender: TObject);
begin
  Map := TMap.CreateAsChild(Scene.Objects);

  Map.Init;
  Map.GLScene := Scene;
  Map.MaterialLibrary := MatLib;
  Map.Material.MaterialLibrary := MatLib;

  Map.Load('House\Level1.m2');
  Map.Build;

  GLSLogo.Position.X := Viewer.Width - GLSLogo.Width / 2;
  GLSLogo.Position.Y := Viewer.Height - GLSLogo.Height / 2;
end;

//------------------------------------------------------------------
procedure TfMain.ViewerMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  mx := X;
  my := Y;
end;

procedure TfMain.ViewerMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  dx, dy: Integer;
begin
  dx := mx - X;
  dy := my - Y;
  if Shift = [ssRight] then
    Camera.MoveAroundTarget(dy, dx);
  if Shift = [ssMiddle] then
    Camera.MoveTargetInEyeSpace(0, dx * 0.12 * Camera.DistanceToTarget /
      Camera.FocalLength, -dy * 0.12 * Camera.DistanceToTarget /
      Camera.FocalLength);
  mx := X;
  my := Y;
end;

procedure TfMain.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
  Camera.AdjustDistanceToTarget(PowerSingle(1.1, WheelDelta / 120));
end;

procedure TfMain.FormResize(Sender: TObject);
begin
  Viewer.Buffer.Render;

  GLSLogo.Position.X := Viewer.Width - GLSLogo.Width / 2;
  GLSLogo.Position.Y := Viewer.Height - GLSLogo.Height / 2;
end;

procedure TfMain.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Viewer);
  FreeAndNil(Scene);
end;


end.
